<?php
$csrfKey = Yii::$app->request->csrfParam;
$csrfVal = Yii::$app->request->csrfToken;
$jsStr = <<<EOF
    var url = '{$delUrl}';
   
    $('#batch-del').on('click', function(){
        var batchBtn = $(this);
        var ids = $('.grid-view').yiiGridView('getSelectedRows');
        if(ids=='') {
            alert('没有选择任何数据');
            return false;
        }
        console.log(ids);
        //window.location.href = url + '&id=' + ids;
        if(confirm("确定删除？")){
            deleteIds(url, ids, batchBtn);
        }
    });
    
    $('.delete').on('click', function(){
        if(confirm("确定删除？")){
            var ids = $(this).attr('deleteid');
            deleteIds(url, ids, $(this));
        }
    })
    
    function deleteIds(url,ids, obj){
        $.ajax({
           url: url,
           type: 'post',
           data: {id: ids,'{$csrfKey}':'{$csrfVal}'},
           beforeSend: function() {
               //obj.html('<span class="glyphicon glyphicon-trash"></span>正在删除...');
               obj.attr('disabled', true);
           },
           complete: function() {
               //obj.html('<span class="glyphicon glyphicon-trash"></span>批量删除');
               obj.removeAttr('disabled');
           },
           error: function() {
               showModelMessage("warning","操作失败",data.msg);
           },
           success: function(data){
               if(data.state==1) {
                    showModelMessage("success","操作成功",data.msg,data.url);
                    return;
               }
               if (data.state==0) {
                    showModelMessage("warning","操作失败",data.msg,data.url);
                    return;
               }
               console.log(data);
           }
        })
    }
EOF;
$this->registerJs($jsStr);